//给你一个二叉树，请你返回其按 层序遍历 得到的节点值。 （即逐层地，从左到右访问所有节点）。 
//
// 
//
// 示例： 
//二叉树：[3,9,20,null,null,15,7], 
//
// 
//    3
//   / \
//  9  20
//    /  \
//   15   7
// 
//
// 返回其层序遍历结果： 
//
// 
//[
//  [3],
//  [9,20],
//  [15,7]
//]
// 
// Related Topics 树 广度优先搜索 二叉树 
// 👍 1135 👎 0

package com.cute.leetcode.editor.cn;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class BinaryTreeLevelOrderTraversal {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(3);
        TreeNode node1 = new TreeNode(9);
        TreeNode node2 = new TreeNode(20);
        TreeNode node3 = new TreeNode(15);
        TreeNode node4 = new TreeNode(7);
        root.left = node1;root.right = node2;node2.left = node3;node2.right = node4;
        new BinaryTreeLevelOrderTraversal().new Solution().levelOrder(root);
    }
    //leetcode submit region begin(Prohibit modification and deletion)
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    /**
     * 层序遍历即为bfs遍历方式
     * 如何保留层数：Queue中是Object数组
     */
    public List<List<Integer>> myLevelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        Queue<Object[]> queue = new LinkedList<>();//0保存节点，1保存层数，层数从1开始
        if (root == null) return res;
        TreeNode node = root;
        queue.add(new Object[]{node, 1});
        while (!queue.isEmpty()){
            node = (TreeNode) queue.peek()[0];
            int count = (int) queue.poll()[1];
            List<Integer> tempList;
            if (count > res.size()) {//这里说明本层需要新建
                tempList = new ArrayList<>();
                tempList.add(node.val);
                res.add(count-1, tempList);
            }else {//说明可以直接拿
                tempList = res.get(count-1);
                tempList.add(node.val);
                res.set(count-1, tempList);
            }
            if (node.left!=null) queue.add(new Object[]{node.left, count + 1});
            if (node.right!=null) queue.add(new Object[]{node.right, count + 1});
        }
        return res;
    }

    /**
     * ！！！根据当前对列的长度进行循环遍历
     */
    public List<List<Integer>> levelOrder(TreeNode root) {
            List<List<Integer>> res = new ArrayList<>();
            Queue<TreeNode> queue = new LinkedList<>();
            if (root!=null) queue.add(root);//添加根节点
            while (!queue.isEmpty()){
                int size = queue.size();//TODO 这里一定要固定循环次数才行
                List<Integer> temp = new ArrayList<>();
                for (int i = 0; i < size; i++) {
                    TreeNode node = queue.poll();
                    temp.add(node.val);
                    if (node.left!=null) queue.add(node.left);
                    if (node.right!=null) queue.add(node.right);
                }
                res.add(temp);
            }
            return res;
        }
}
//leetcode submit region end(Prohibit modification and deletion)
static class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(){}
    TreeNode(int val){this.val = val;}
    TreeNode(int val, TreeNode left, TreeNode right){
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

}
































